<template>

    <van-pull-refresh v-model="refreshing" @refresh="OnRefresh">
        <van-list 
        v-model:loading="loading" 
        :finished="finished" 
        finished-text="没有更多了" 
        @load="OnLoad"
        >

            <div class="coupon_list">
                <div class="coupon_item" v-for="item in list">
                    <div class="item_content">
                        <router-link to="/coupon/info">
                            <div class="left">{{ item.rate }}<span>折</span></div>
                            <div class="right">
                                <div>{{ item.title }}</div>

                                <div>订单满<span>100元</span>打<span>{{ item.rate }}折</span></div>

                                <div>每人限领1张</div>
                                <div>领取后10天内有效</div>
                            </div>
                        </router-link>
                    </div>
                    <div class="item_btn">
                        <a class="right_btn">优惠详情</a>
                    </div>
                </div>
            </div>

        </van-list>
    </van-pull-refresh>
</template>
  
<script setup>
import { ref } from 'vue';


const { proxy } = getCurrentInstance();

const business = proxy.$business;

const loading = ref(false);
const finished = ref(false);
const refreshing = ref(false);

//分页
const page = ref(1);

//数据列表
const list = ref([]);

//挂载
const OnRefresh = async () => {
    list.value = [];
    page.value = 1;
    refreshing.value = true;
    loading.value = true;
    finished.value = false;

    setTimeout(OnLoad, 1500);
}

//加载
const OnLoad = async () => {
    if (refreshing.value === true) {
        refreshing.value = false;
    }

    //组装数据
    const params = {
        busid: business.value.id,
        page: page.value,
    }

    //请求数据
    const result = await proxy.$http.post('/coupon/index', params);

    loading.value = false;

    if (result.code === 1) {
        let count = result.data.count;
        list.value = list.value.concat(result.data.list);

        if (list.value.length >= count) {
            finished.value = true;

            return false;
        }

        page.value  += 1;
    }else {
        finished.value = true;

        proxy.$fail(result.msg);
    }

}



</script>

<style scoped>
@import url('/assets/css/CouponIndex.css');
</style>